using UnityEngine;
using System.Collections;

public class ItemVisibility : MonoBehaviour 
{
    public bool isVisible;
    public Transform player;
    public Transform look;
    public bool canSee;
    public bool haveCalled;
    public bool isActive;
    public Material reqMat;

    void Update()
    {
        if (renderer.material == reqMat || reqMat == null || renderer.material.name.Contains(reqMat.name))
        {
            isActive = true;
        }
        else
        {
            isActive = false;
        }

        look.transform.LookAt(player.position);
        RaycastHit hit;
        if (Physics.Raycast(look.position, look.forward, out hit))
        {
            if (hit.collider.gameObject.transform == player)
            {
                canSee = true;
            }
            else
            {
                canSee = false;
                if(!haveCalled)
                {
                    haveCalled = false;
                }
            }
        }
        if (renderer.IsVisibleFrom(Camera.main) && canSee)
        {
            isVisible = true;
        }
        else
        {
            isVisible = false;
        }

        if (canSee && isVisible && !haveCalled)
        {
            haveCalled = true;
            ++ItemCount.numberOfItems;
        }

        if ((!canSee || !isVisible) && haveCalled)
        {
            haveCalled = false;
            --ItemCount.numberOfItems;
        }
    }
}
